package com.yunhe.marketing.util;

import com.yunhe.common.constant.SystemConsts;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 请求头工具类
 * @author liuronglei
 */

@Component
public class MyHeaderUtil {

    public Long getHeaderUser() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userIdStr = request.getHeader(SystemConsts.HEADER.USER_ID.value());
        if (StringUtils.isEmpty(userIdStr)) {
            return null;
        }
        Long userId = Long.parseLong(userIdStr);
        return userId;
    }

    public String getIp() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = request.getHeader(SystemConsts.HEADER.REMOTE_ADDRESS.value());
        if (StringUtils.isEmpty(ip)) {
            return null;
        }
        return ip;
    }
}
